<?php
/**
 * 年级
 * Created by Zhy
 * User: Zhy
 * Date: 2019/4/1
 * Time: 9:46
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Grade extends Base
{
    /**
     * 年级首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $where['grade_del'] = 1;
            $count = Db::name('grade')->where($where)->count();// 获取总条数
            $lists = Db::name('grade')
                ->field('grade_id, grade_name, grade_status, grade_time, grade_del, grade_update_time, 
                grade_sort')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('grade_sort')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加年级
     * @return mixed|\think\response\Json
     */
    public function gradeAdd(){
        if(request()->isPost()){
            $grade_name = trim(input('grade_name'), ' ');
            //判断在冻结期间有没有添加相同名称的年级，有则不给用
            $grade = Db::table('up_grade')
                ->where('grade_del', 1)
                ->where('grade_status', 1)
                ->where('grade_name', $grade_name)
                ->find();
            if($grade){
                return json(['code' => 100, 'msg' => '已有相同名称的年级，不可添加']);
            }
            
            $data = [
                'grade_name' => $grade_name,
                'grade_time' => date('Y-m-d H:i:s'),
                'grade_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_grade')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('gradeadd');
    }
    
    /**
     * 编辑年级
     * @return mixed|\think\response\Json
     */
    public function gradeEdit(){
        if(Request::instance()->isPost()){
            $grade_id = input('grade_id');
            $grade_name = trim(input('grade_name'), ' ');
            if(empty($grade_name)){
                return json(['code' => 100, 'msg' => '年级名称不能为空']);
            }
            //判断在冻结期间有没有添加相同名称的年级，有则不给用
            $grade = Db::table('up_grade')
                ->where('grade_del', 1)
                ->where('grade_status', 1)
                ->where('grade_name', $grade_name)
                ->find();
            if($grade){
                return json(['code' => 100, 'msg' => '已有相同名称的年级，不可编辑']);
            }
            $data = [
                'grade_name' => $grade_name,
                'grade_update_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_grade')->where('grade_id', $grade_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $grade_id = input('id');
        $grade_name = Db::table('up_grade')->where('grade_id', $grade_id)->value('grade_name');
        $this->assign([
            'grade_name' => $grade_name,
            'grade_id' => $grade_id
        ]);
        return $this->fetch('grade/gradeedit');
    }
    
    /**
     * 状态修改
     * @return \think\response\Json
     */
    public function gradeStatus(){
        $grade_id = input('id');
        $grade_status = input('num');
        //判断在冻结期间有没有添加相同名称的年级，有则不给用
        $grade_name = Db::table('up_grade')->where('grade_id', $grade_id)->value('grade_name');
        $grade = Db::table('up_grade')
            ->where('grade_del', 1)
            ->where('grade_status', 1)
            ->where('grade_id', 'neq', $grade_id)
            ->where('grade_name', $grade_name)
            ->find();
        if($grade){
            return json(['code' => 100, 'msg' => '已有相同名称的年级，不可启用']);
        }
        $res = Db::table('up_grade')->where('grade_id', $grade_id)->setField('grade_status', $grade_status);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除年级
     * @return \think\response\Json
     */
    public function gradeDel(){
        $grade_id = input('id');
        $is_have = Db::table('up_grade_class')->where('gs_grade_id', $grade_id)->find();
        $is_book = Db::table('up_reading_book')->where('rb_grade_id', $grade_id)->find();
        if(!empty($is_have) || !empty($is_book)){
            return json(['code' => 100, 'msg' => '请先移除该年级下的关联项']);
        }
        $res = Db::table('up_grade')->where('grade_id', $grade_id)->setField('grade_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 完善信息后获取的书籍
     * @return mixed
     */
    public function bookAdd(){
        if(request()->isPost()){
            $params = input('param.');
            $res = Db::table('up_grade')
                ->where('grade_id', $params['grade_id'])
                ->update(['grade_books' => $params['rb_id']]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $grade_id = input('id');
        //查找所属年级的书籍
        if($grade_id == 13){//其他
            $where = [];
        }else{
            $where['rb_grade_id'] = $grade_id;
        }
        $where['rb_type'] = 2;//指定书籍
        $where['rb_status'] = 1;
        //年级对应的书籍
        $grade_books = Db::table('up_reading_book')
            ->field('rb_id, rb_name')
            ->where($where)
            ->select();
        //已分配的书籍
        $book_ids = Db::table('up_grade')->where('grade_id', $grade_id)->value('grade_books');
//        $book_ids = explode(',', $book_ids);
        $this->assign([
            'grade_books' => $grade_books,
            'book_ids' => $book_ids,
            'grade_id' => $grade_id
        ]);
        return $this->fetch('grade/bookadd');
    }
    
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        if(request()->isPost ()){
            $id = input('id');
            $param = [
                'grade_sort'=>input('value'),
            ];
            $res = Db::name('grade')->where('grade_id',$id)->update($param);
            if($res){
                return json(['code' => 200, 'msg' => '修改成功']);
            }else{
                return json(['code' => 100, 'msg' =>' 修改失败']);
            }
        }
    }
}